
package reggex;

import java.util.Iterator;
import java.lang.RuntimeException;

import lexer.Token;
import lexer.Tokenizer;

public class ReggexTokenizer implements Tokenizer {
	
	public static final String ALL_CHARACTERS =
		"ABCDEFGHIJKLMNÑOPQRSTUVWXYZ" +
		"abcdefghijklmnñopqrstuvwxyz" +
		"01234567890 "
	;
	
	public static final int CHAR_ID = 1;
	public static final int OPER_ID = 2;
	public static final int PAR1_ID = 3;
	public static final int PAR2_ID = 4;
	public static final int PIPE_ID = 5;
	
	
	public Iterator<Token> getTokens(Iterator<Character> input) {
		return new It(input);
	}
	
	private static class It implements Iterator<Token> {
		private final Iterator<Character> input;
		
		private It(Iterator<Character> input) {
			this.input = input;
		}
		
		public Token next() {
			Character c = input.next();
			if(ALL_CHARACTERS.indexOf(c.toString()) >= 0) {
				return new Token(CHAR_ID, c.toString());
			} else if('.' == c) {
				return new Token(CHAR_ID, c.toString());
			} else if('*' == c || '+' == c || '?' == c) {
				return new Token(OPER_ID, c.toString());
			} else if('(' == c) {
				return new Token(PAR1_ID, c.toString());
			} else if(')' == c) {
				return new Token(PAR2_ID, c.toString());
			} else if('|' == c) {
				return new Token(PIPE_ID, c.toString());
			} else {
				throw new IllegalArgumentException("Invalid terminal: " + c);
			}
		}
		
		public boolean hasNext() {
			return input.hasNext();
		}
		
		public void remove() {
			throw new RuntimeException(); // TODO cambiar tipo de exc.
		}
	}
}
